package com.employeemanagementsystem.controller;

import com.employeemanagementsystem.dao.UserDao;
import com.employeemanagementsystem.model.User;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/delete_user")
public class DeleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null && "ADMIN".equalsIgnoreCase(user.getRole())) {
            int userId = Integer.parseInt(request.getParameter("userId"));
            UserDao userDao = new UserDao();

            if (userDao.deleteUser(userId)) {
                response.sendRedirect("admin_home?message=User deleted successfully!");
            } else {
                response.sendRedirect("admin_home?error=Failed to delete user.");
            }
        } else {
            response.sendRedirect("login.jsp?error=Please log in as an administrator.");
        }
    }
}
